'use strict';
/////////////////////////////////////
// 统一的缓存控制控件
// 应用于某页面跳转后执行操作再返回 保留有效信息
// 流程 页面A-执行初始化-读接口同时缓存信息(A=1)-完成-跳转(A=0)
// -页面B-声明不删除A(A=1)-业务操作-跳转(A=0)
// -页面A-执行初始化-执行读缓存(A只要不比0小就可以)
/////////////////////////////////////
function $ctrlCache($state, $stateParams) {
    var saveParam = []; // {route: 'xxx', param: {}, time:1}
    var obj = {
        saveCache: function (param) {
            var route = $state.current.name;
            console.log('页面缓存', '存', route, param)
            for(var i = 0, l = saveParam.length; i < l; i++){
                if ( route == saveParam[i]['route']) {
                    saveParam[i]['time'] = 1;
                    saveParam[i]['param'] = angular.copy(param);
                    return;
                }
            }
            var one = {};
            one.route = route;
            one.param = angular.copy(param);
            one.time = 1;
            saveParam.push(one);
        },
        getCache: function () {
            var route = $state.current.name;
            for(var i = 0, l = saveParam.length; i < l; i++){
                if ( route == saveParam[i]['route'] && saveParam[i]['time'] >= 0 ) {
                    saveParam[i]['time'] -= 1;
                    console.log('页面缓存', '取', saveParam[i]['param'])
                    return angular.copy(saveParam[i]['param']);
                }
            }
            console.log('页面缓存', '取', null)
            return null;
        },
        prolongCache: function (route) {
            console.log('页面缓存', '续', route)
            for(var i = 0, l = saveParam.length; i < l; i++){
                if ( route == saveParam[i]['route'] ) {
                    saveParam[i]['time'] += 1;
                }
            }
        },
        routeRemove: function (param) {
            for(var l = saveParam.length - 1; l >= 0; l--){
                saveParam[l]['time'] -= 1;
                if ( saveParam[l]['time'] < 0 ) {
                    saveParam.splice(l, 1)
                }
            }
            console.log('页面缓存', '移除', JSON.stringify(saveParam))
        }
    }
    return obj;
}
/*
app.factory('$ctrlUrl2', function($state,$stateParams) {
    var obj = {
        modifyUrl : function( option ){
            var url = location.href.replace( /\?.*$/, "" );
            $.extend(true, option, $stateParams);
            var param = _Url_obj2url( option );
            location.href = url+"?"+param;
        },
        modifyObjFromUrl : function( arr ){
            var p = _Url_url2obj( location.href );
            for( var i1 in arr){
                for( var i2 in arr[i1] ){
                    if( p[i2] ) arr[i1][i2] = p[i2];
                }
            }
        }
    };
    return obj;
    function _Url_url2obj(e) {
        var t = {},
            r = e.substr(1 + e.indexOf("?"));
        r = r.split("&");
        for (var n = 0; n < r.length; n++) { var o = r[n].split("="); t[o[0]] = decodeURIComponent(o.slice(1).join("=")) }
        return t
    }
    function _Url_obj2url(data) {
        var arr = [], en = encodeURIComponent;
        for (var name in data) { arr.push(en(name)+'='+en(data[name])) }
        return arr.join('&');
    }
});

app.factory('$ctrlUrl3', function($localStorage) {
    var $ls = $localStorage.$default();
    var obj = {
        //参数格式t:{p:obj},f:{p:obj}
        //参数保存入口
        save : function( type, name, param ){
            name = name.replace(/\?.*$/g,"");
            $ls[type][name] = param;
            return obj;
        },
        //表格的参数保存入口
        saveTableParam : function( name, param ){
            name = name.replace(/\?.*$/g,"");
            return obj.save( "tp", name, param );
        },
        //表单的参数保存入口
        saveFormParam : function( name, param ){
            name = name.replace(/\?.*$/g,"");
            return obj.save( "fp", name, param );
        },
        //参数读取入口
        getParam : function( name ){
            name = name.replace(/\?.*$/g,"");
            return $ls["tp"][name] || $ls["fp"][name] || {};
        },
        //参数继承关系
        extend : function( option , p1 , p2 , p3 ){
            option = option || {}; p1 = p1 || {}; p2 = p2 || {}; p3 = p3 || {};
            function gr(v){ return v!==undefined&&v!==null }
            for( var i in option ){
                option[i] = gr(p3[i]) ? p3[i] : gr(p2[i]) ? p2[i] : gr(p1[i]) ? p1[i] : option[i];
            }
            return option;
        },
        //初始化两个参数
        initParam : function( ){
            $ls["tp"] = $ls["tp"] || {};
            $ls["fp"] = $ls["fp"] || {};
            return obj;
        },
        //清空所有参数
        clearParam : function( ){
            $ls["tp"] = {};
            $ls["fp"] = {};
            return obj;
        },
        //清空表格参数
        removeTableParam : function( name ){
            name = name.replace(/\?.*$/g,"");
            if( $ls["tp"][name] ) $ls["tp"][name] = null;
            return obj;
        },
        //清空表单参数
        removeFormParam : function( name ){
            name = name.replace(/\?.*$/g,"");
            if( $ls["fp"][name] ) $ls["fp"][name] = null;
            return obj;
        },
        //清除某条参数
        removeParam : function( name ){
            name = name.replace(/\?.*$/g,"");
            if( $ls["tp"][name] ) $ls["tp"][name] = null;
            if( $ls["fp"][name] ) $ls["fp"][name] = null;
            return obj;
        }
    };
    return obj.initParam();
});
*/
exports = module.exports = $ctrlCache
